#pragma once

#include <QSqlDatabase>
#include <QTcpServer>
#include "Client.h"
#include "Protocol.h"

typedef QHash<QString, Client*> OnlineUsers;

// 服务器
class Server: public QTcpServer {
    Q_OBJECT

    friend class Client;

    private: QSqlDatabase mDatabase;    // 数据库
    private: OnlineUsers mOnlineUsers;  // 在线用户列表

    public: explicit Server();
    public: ~Server();
    private slots: void onAccept();
    private: void createDatabase();
    private: void handleAddFriendRequest(AddFriendRequest*, Client*);
    private: void handleAddFriendResult(AddFriendResult*);
    private: void handleChat(ChatInfo*);
    private: void handleLogoff(LogoffRequest*);
    private: void handleLogon(LogonRequest*, Client*);
    private: void handleReg(RegRequest*, Client*);
    public: void onGetMessageFromClient(ImProtocol*, Client*);
    private: void sendData(Client*, ImProtocol*, qint64);
    public: bool start();
};
